package com.zmn.brs.admin.controller.blacklist;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.logger.LoggerUtil;
import com.zmn.common.utils.office.ExcelUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.brs.model.entity.blacklist.Blacklist;
import com.zmn.brs.model.entity.blacklist.query.BlacklistQuery;
import com.zmn.brs.services.interfaces.blacklist.BlacklistService;
import com.zmn.brs.services.interfaces.whitelist.WhitelistService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述: 黑名单逻辑控制类
 *
 * @author wangjie
 * @since 2019-07-31
 **/
@Controller
@RequestMapping(value = "/blacklist/")
public class BlacklistModifyController extends BlacklistBaseController {

    public BlacklistModifyController(BlacklistService blacklistService, WhitelistService whitelistService) {
        super(blacklistService, whitelistService);
    }

    /**
     * 描述: 添加黑名单对象
     *
     * @param blacklist
     *            黑名单数据对象
     * @return com.zmn.common.dto.ResultDTO
     **/
    @ResponseBody
    @RequestMapping(value = "save_blacklist")
    public ResponseDTO saveBlacklist(@RequestBody Blacklist blacklist) {

        try {
            ResponseDTO resultDto =
                super.checkBlacklistAndWhitelistByBlackNumAndBlackType(blacklist.getBlacklistNum(), blacklist.getBlacklistType());
            if (!resultDto.isSuccess()) {
                return resultDto;
            }
            LoginStaff mcStaff = super.getMcStaff();
            blacklist.setCreater(mcStaff.getRealName());
            blacklistService.insertBlack(blacklist);
            return ResponseDTO.success(true,"添加黑名单对象成功");
        } catch (Exception e) {
            logger.error("添加黑名单对象错误,e{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("添加黑名单对象错误");
        }
    }

    /**
     * 描述: 根据黑名单id 类型 删除数据
     *
     * @param blacklist
     *            黑名单数据对象
     * @return com.zmn.common.dto.ResultDTO
     **/
    @ResponseBody
    @RequestMapping(value = "delete_blacklist")
    public ResponseDTO deleteBlacklist(Blacklist blacklist) {

        try {
            blacklistService.deleteBlackByIdAndType(blacklist.getBlacklistId(), blacklist.getBlacklistType());
            return ResponseDTO.success(true,"删除黑名单数据成功");
        } catch (Exception e) {
            logger.error("删除黑名单数据错误,e{},msg{}", e, e.getMessage());
            return ResponseDTO.success(true,"删除黑名单数据错误");
        }
    }

    /**
     * 描述: 黑名单数据导出
     *
     * @param response
     *            响应
     * @param query
     *            查询对象
     * @author wangjie
     * @since 2019/8/2
     **/
    @RequestMapping(value = "outputExcel")
    // @RequiresPermissions(value = "/blacklist/outputExcel.action")
    public void outputExcel(HttpServletResponse response, @ModelAttribute BlacklistQuery query) {
        Integer pageSize = getExportPageLimit(response);
        query.setPageSize(pageSize);

        ArrayList<List> lists = super.outputExcel(query);
        try {
            String name = "黑名单报表.xlsx";
            name = new String(name.getBytes(), "ISO8859-1");
            ExcelUtil.export(response, lists, name, "sheet");
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "导出报表出错" + e.getMessage(), e);
        }
    }
}